package com.trt.beibei.api.v1;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.trt.beibei.exception.http.NotFoundException;
import com.trt.beibei.model.Theme;
import com.trt.beibei.service.ThemeService;
import com.trt.beibei.vo.ThemePureVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author tanrentao
 * @date 2021年08月09日 10:42
 * @desc 主题控制器
 */
@Validated
@RestController
@RequestMapping("/theme")
public class ThemeController {

    @Autowired
    private ThemeService themeService;

    /**
     * 根据 names 查询主题列表
     * @param names
     * @return
     */
    // @ScopeLevel(8)
    @GetMapping("/by/names")
    public List<ThemePureVO> getThemeGroupByNames(@RequestParam(name = "names") String names) {
        List<String> nameList = Arrays.asList(names.split(","));
        List<Theme> themes = themeService.findByNames(nameList);
        List<ThemePureVO> list = new ArrayList<>();
        themes.forEach(theme -> {
            Mapper mapper = DozerBeanMapperBuilder.buildDefault();
            ThemePureVO vo = mapper.map(theme, ThemePureVO.class);
            list.add(vo);
        });
        return list;
    }

    /**
     * 根据 themeName 查询主题信息
     * @param themeName
     */
    //VIP分组 16
    //User分组 2
    @GetMapping("/name/{name}/with_spu")
    public Theme getThemeByNameWithSpu(@PathVariable(name = "name") String themeName){
        Optional<Theme> optionalTheme = themeService.findByNames(themeName);
        return optionalTheme.orElseThrow(() -> new NotFoundException(30003));
    }


}
